package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.common.ErrorType;
import com.engineering.elink.service.VideoCollectionService;
import com.engineering.elink.util.ResJsonUtil;

@RestController
@CrossOrigin
@RequestMapping("/api/videocollection")
@Api("课程收藏接口")
@ApiIgnore
public class VideoCollectionController {
	
	@Autowired
	private VideoCollectionService videoCollectionService;
	
	/**
	 * 用户收藏及取消课程
	 */
	@RequestMapping(value="/update",method=RequestMethod.GET)
	@ApiOperation(value="用户收藏课程")
	public Map<String, Object> update(@ApiParam(value = "课程id", required = true) @RequestParam("courseId") Integer courseId,HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		
		String userId=null;
		if(userInfo!=null && userInfo.get("userId")!=null){
			userId =  userInfo.get("userId").toString();
		}
		
		return videoCollectionService.updateCollect(userId, courseId);
	}
	
	/**
	 * 获取我的课程收藏，支持分页
	 */
	@RequestMapping(value="/myCollect",method=RequestMethod.GET)
	@ApiOperation(value="我的课程收藏")
	public Map<String, Object> getMyCollect(@ModelAttribute @Valid BaseCo co,HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		return ResJsonUtil.getMap(ErrorType.SUCCESS,videoCollectionService.getMyCollect(co,userId));
	}
}
